package com.j2eefast.bussiness.contract.service;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.bussiness.contract.entity.BusEquipmentEntity;

import com.j2eefast.bussiness.contract.mapper.BusEquipmentMapper;

import com.j2eefast.framework.code.service.SerialService;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>设备管理</p>
 *
 * @author: summer
 * @date: 2021-05-31 14:41
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */

@Service
public class BusEquipmentService extends ServiceImpl<BusEquipmentMapper, BusEquipmentEntity> {

    @Resource
    protected BusEquipmentMapper busEquipmentMapper;
    @Resource
    @Lazy
    private SerialService serialService;

    /**
     * 页面展示查询翻页
     */
    public PageUtil findPage(Map<String, Object> params) {
        String beginTime=(String) params.get("beginTime");
        String endTime=(String) params.get("endTime");
        String useDepartment = (String) params.get("useDepartment");
        String safekeeping = (String) params.get("safekeeping");
        String equipmentCode = (String) params.get("equipmentCode");
        String equipmentName = (String) params.get("equipmentName");
        String equipmentModel = (String) params.get("equipmentModel");
        String manufacture = (String) params.get("manufacture");
        String usages = (String) params.get("usages");
        String remark = (String) params.get("remark");
        String depositAddr = (String) params.get("depositAddr");
        String equipmentValue = (String) params.get("equipmentValue");
        Page<BusEquipmentEntity> page = busEquipmentMapper.findPage(
                new Query<BusEquipmentEntity>(params).getPage(),
                StrUtil.nullToDefault(beginTime,""),
                StrUtil.nullToDefault(endTime,""),
				StrUtil.nullToDefault(equipmentModel,""),
                StrUtil.nullToDefault(manufacture,""),
                StrUtil.nullToDefault(depositAddr,""),
                StrUtil.nullToDefault(useDepartment,""),
                StrUtil.nullToDefault(safekeeping,""),
                StrUtil.nullToDefault(remark,""),
                StrUtil.nullToDefault(usages,""),
                StrUtil.nullToDefault(equipmentCode,""),
                StrUtil.nullToDefault(equipmentName,""),
                StrUtil.nullToDefault(equipmentValue,""),
                (String) params.get(Constant.SQL_FILTER));
        return new PageUtil(page);
    }

    public boolean deleteByIds(Long[] ids) {

        return busEquipmentMapper.deleteByIds(ids);

    }

   public BusEquipmentEntity equipmentInfo(int id){
       return busEquipmentMapper.equipmentInfo(id);
    }

    public boolean updateCheck(Long id) {
        Long userId= UserUtils.getUserInfo().getId();

        return busEquipmentMapper.updateCheck(id ,userId);

    }
    public SysUserEntity getSysById(@Param("userId")Long userId){
        return busEquipmentMapper.getSysById(userId);
    }


//    @DataFilter(compAlias="c",deptAlias = "d",userAlias = "u")
//    public List<BusEquipmentEntity> findList(Map<String, Object> params) {
////        String name = (String) params.get("name");
////        String ceoName = (String) params.get("ceoName");
////        String paymentNuit = (String) params.get("paymentNuit");
////        String contractSum = (String) params.get("contractSum");
////        String projectName = (String) params.get("projectName");
////        Long[] ids  = (Long[]) params.get("ids");
//        return busEquipmentMapper.findList(
////                StrUtil.nullToDefault(name,""),
////                StrUtil.nullToDefault(ceoName,""),
////                StrUtil.nullToDefault(paymentNuit,""),
////                StrUtil.nullToDefault(projectName,""),
////                StrUtil.nullToDefault(contractSum,""),
////                ids,
//                (String) params.get(Constant.SQL_FILTER));
//    }


    /**
     * 页面展示查询翻页
     */
    public List<BusEquipmentEntity> findList(Map<String, Object> params) {
        String beginTime=(String) params.get("beginTime");
        String endTime=(String) params.get("endTime");
        String equipmentType = (String) params.get("equipmentType");
        String meteringUnit = (String) params.get("meteringUnit");
        String depositAddr = (String) params.get("depositAddr");
        String useDepartment = (String) params.get("useDepartment");
        String safekeeping = (String) params.get("safekeeping");
        String increase = (String) params.get("increase");
        String usages = (String) params.get("usages");
        String equipmentCode = (String) params.get("equipmentCode");
        String equipmentName = (String) params.get("equipmentName");
        String accumulatedDepreciation = (String) params.get("accumulatedDepreciation");
        //String parentId = (String) params.get("parentId");
        List<BusEquipmentEntity> page = busEquipmentMapper.findList(
                new Query<BusEquipmentEntity>(params).getPage(),
                StrUtil.nullToDefault(beginTime,""),
                StrUtil.nullToDefault(endTime,""),
                StrUtil.nullToDefault(equipmentType,""),
                StrUtil.nullToDefault(meteringUnit,""),
                StrUtil.nullToDefault(depositAddr,""),
                StrUtil.nullToDefault(useDepartment,""),
                StrUtil.nullToDefault(safekeeping,""),
                StrUtil.nullToDefault(increase,""),
                StrUtil.nullToDefault(usages,""),
                StrUtil.nullToDefault(equipmentCode,""),
                StrUtil.nullToDefault(equipmentName,""),
                StrUtil.nullToDefault(accumulatedDepreciation,""),
                //StrUtil.nullToDefault(parentId,""),
                (String) params.get(Constant.SQL_FILTER));
        return page;
    }


    public boolean saves(BusEquipmentEntity taskFrom){
                Date date = new Date();//创建一个时间对象，获取到当前的时间
        taskFrom.setCreateTime(date);
        String userName=UserUtils.getUserInfo().getName();
        taskFrom.setCreateBy(userName);
        BusEquipmentEntity pmsTestProjectEntity = busEquipmentMapper.selectEquipment();
        Long total=0L;
        if(pmsTestProjectEntity!=null && pmsTestProjectEntity.getTotal()!=null){
            total=pmsTestProjectEntity.getTotal();
        }

        Map<String ,String> participant= serialService.formatTitle(total,"设备编号序列");//编号
        taskFrom.setEquipmentCode(participant.get("title"));
        taskFrom.setTotal(Long.parseLong(participant.get("total")));
        return this.save(taskFrom);
       }


    public int getEquipmentInfo(String equipmentCode) {
        BusEquipmentEntity busEquipmentEntity = busEquipmentMapper.getEquipmentInfo(equipmentCode);
        if(busEquipmentEntity!=null){
            return 1;
        }else
            return 0;
    }
}
